<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h2>Object常用方法</h2>
</body>
<script>
    //1.Object.assign
    const obj1 = {
        name: 'alice',
        age: 20,
        hobby: ['游泳', '打球']
    }
    const obj2 = {
        address: '北京'
    }

    const res = Object.assign({}, obj1, obj2)
        // const res2 = {...obj1,
        //     ...obj2
        // }

    // res.hobby[0] = '跑步'

    // console.log(obj1)
    // console.log(res)

    //2.Object.create
    // const obj = {
    //     name: 'alice',
    //     age: 20
    // }

    // const result = Object.create(obj)
    // console.log('result:', result)
    // console.log('result:', result.age)

    //3.Object.defniePropety
    const obj = {}
    Object.defineProperty(obj, 'name', {
        // value: '张三',
        // writable: true,
        // enumerable: true,

        //获取值
        get() {
            console.log('正在获取值')
            return val;
        },
        //设置值
        set(newVal) {
            console.log('正在赋值')
            val = newVal
            console.log('赋值完成')
        }

    })


    obj.name = '李四'
    console.log(obj.name)

    // for (let key in obj) {
    //     console.log('key:', key)
    // }
</script>

</html>